<template>
	<view class="detail">
		<view class="container">
			<view v-if="loadingState">
				<u-skeleton rows="10" title loading></u-skeleton>
			</view>
			<view class="title">{{ detailObj.title }}</view>
			<view class="userinfo">
				<view class="avatar">
					<image
						:src="detailObj.user_id[0].avatar_file ? detailObj.user_id[0].avatar_file.url : '../../static/images/avatar.jpg'"
						mode="aspectFill"></image>
				</view>
				<view class="text">
					<view class="name">
						{{detailObj.user_id[0].nickname ? detailObj.user_id[0].nickname : detailObj.user_id[0].username}}
					</view>
					<view class="small">
						<uni-dateformat :date="detailObj.publish_date" format="MM月dd hh:mm"
							:threshold="[60000,3600000*24*30]"></uni-dateformat>
						· 发布于{{ detailObj.province }}
					</view>
				</view>
			</view>
			<view class="content">
				<u-parse :content="detailObj.content" :tagStyle="tagStyle"></u-parse>
			</view>

			<view class="like">
				<view class="btn" @click="clickLike">
					<text class="iconfont icon-good-fill"></text>
					<text>{{ detailObj.like_count }}</text>
				</view>
				<view class="users">
					<image src="../../static/images/avatar.jpg" mode="aspectFill"></image>
				</view>
				<view class="text"><text class="num">{{ detailObj.view_count }}</text>人看过</view>
			</view>
		</view>
	</view>
</template>

<script>
	const db = uniCloud.database();
	const utilsObj = uniCloud.importObject("utilsObj");
	export default {
		data() {
			return {
				artid: "",
				tagStyle: {
					p: "line-height:1.7em;font-size:16px;padding-bottom:10rpx",
					img: "margin:10px 0"
				},
				detailObj: null,
				loadingState: true, // 骨架屏是否显示 / 加载动画v-if控制
			};
		},
		onLoad(e) {
			if (!e.id) {
				this.errFun("参数有误");
				return;
			}
			this.artid = e.id;
			this.getData();

			this.readUpdate(); // 修改阅读量
		},
		methods: {
			// 点赞
			async clickLike() {
				let res = await db.collection("quanzi_like").where(
					`article_id=='${this.artid}' && user_id==$cloudEnv_uid`).count();
				console.log(res);

				if (res.result.total) {
					utilsObj.operation("quanzi_article", "like_count", this.artid, 0).then(res => {
						console.log(res);
					})
				} else {
					db.collection("quanzi_like").add({
						article_id: this.artid
					})
				}

			},
			// 阅读量更新
			readUpdate() {
				utilsObj.operation("quanzi_article", "view_count", this.artid, 3).then(res => {
					console.log(res);
				})
			},
			// 错误处理
			errFun(msg) {
				uni.showToast({
					title: msg,
					icon: "error"
				})

				setTimeout(() => {
					uni.reLaunch({
						url: "/pages/index/index"
					})
				}, 1500);
			},
			// 初始化数据
			getData() {
				let artTemp = db.collection("quanzi_article").getTemp();
				let userTemp = db.collection("uni-id-users").field("_id,username,nickname,avatar_file").getTemp();

				db.collection(artTemp, userTemp).where(`_id=='${this.artid}'`).get({
					getOne: true
				}).then(res => {

					// id非空却错误,res.result没data这属性
					if (!res.result.data) {
						this.errFun("该文章不存在");
						return;
					} else {
						this.loadingState = false;
						this.detailObj = res.result.data;
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	.detail {
		background: #f8f8f8;
		min-height: calc(100vh - var(--window-top));

		.container {
			padding: 30rpx;
			background: #fff;

			.title {
				font-size: 46rpx;
				color: #333;
				line-height: 1.4em;
				font-weight: 600;
			}

			.userinfo {
				padding: 20rpx 0 50rpx;
				display: flex;
				align-items: center;

				.avatar {
					width: 60rpx;
					height: 60rpx;
					padding-right: 15rpx;

					image {
						width: 100%;
						height: 100%;
						border-radius: 50%;
					}
				}

				.text {
					font-size: 28rpx;
					color: #555;

					.small {
						font-size: 20rpx;
						color: #999;
					}
				}
			}

			.content {}

			.like {
				display: flex;
				flex-direction: column;
				align-items: center;
				padding: 80rpx 50rpx 50rpx;

				.btn {
					width: 260rpx;
					height: 120rpx;
					background: #e4e4e4;
					border-radius: 100rpx;
					color: #fff;
					display: flex;
					justify-content: center;
					align-items: center;
					flex-direction: column;
					font-size: 28rpx;

					.iconfont {
						font-size: 50rpx;
					}

					&.active {
						background: #0199FE;
					}
				}

				.text {
					font-size: 26rpx;
					color: #666;

					.num {
						color: #0199FE
					}

					.spot {
						padding: 0 10rpx;
					}
				}

				.users {
					display: flex;
					justify-content: center;
					padding: 30rpx 0;

					image {
						width: 50rpx;
						height: 50rpx;
						border-radius: 50%;
						border: 3px solid #fff;
						margin-left: -20rpx;
					}
				}
			}

		}
	}
</style>